package com.pic4493.app;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import com.pic4493.adapter.AdpList_ShareChooser;
import com.pic4493.base.BaseUI;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class UIShareChooser extends BaseUI
{
   private ListView mLvChooser;
   private TextView mTvTitle;
   private TextView mTvNoContent;

   private List<HashMap<String, Object>> mChooserList;
   private Intent mPassIntent;

   private final String[] ACCEPT_LIST = { "com.sina.weibo", "com.tencent.mobileqq","com.qzone","com.tencent.mm", "com.android.mms", "im.yixin" };// "com.sina.weibo.EditActivity"
   private final String[] IGNORE_LIST = {};

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	  super.onCreate(savedInstanceState);
	  requestWindowFeature(Window.FEATURE_NO_TITLE);
	  setContentView(R.layout.comm_module_chooser);

	  Intent intent = getIntent();
	  String title = intent.getStringExtra(Intent.EXTRA_TITLE);
	  mPassIntent = intent.getParcelableExtra(Intent.EXTRA_INTENT);

	  mLvChooser = (ListView) findViewById(R.id.lvChooser);	  mTvTitle = (TextView) findViewById(R.id.tvTitle);
	  mTvNoContent = (TextView) findViewById(R.id.tvNoContent);

	  if (title != null)
		 mTvTitle.setText(title);
	  else
		 mTvTitle.setText("");

	  PackageManager pm = getPackageManager();
	  List<ResolveInfo> resList = pm.queryIntentActivities(mPassIntent, PackageManager.MATCH_DEFAULT_ONLY);

	  ResolveInfo.DisplayNameComparator rComparator = new ResolveInfo.DisplayNameComparator(pm);
	  Collections.sort(resList, rComparator);

	  mChooserList = new ArrayList<HashMap<String, Object>>();
	  for (ResolveInfo res : resList)
	  {
		 String activityName = res.activityInfo.name;
		 if (isActivityAccept(res.activityInfo.packageName))
		 {
			HashMap<String, Object> activityInfoMap = new HashMap<String, Object>();
			activityInfoMap.put("AppIcon", res.loadIcon(pm));
			activityInfoMap.put("AppLabel", res.loadLabel(pm));
			activityInfoMap.put("AppPackageName", res.activityInfo.applicationInfo.packageName);
			activityInfoMap.put("AppActivityName", activityName);
			if (activityName.equals("com.android.mms.ui.ComposeMessageActivity"))
			{
			   mChooserList.add(0, activityInfoMap);
			}
			else
			   mChooserList.add(activityInfoMap);
		 }
	  }
	  if (mChooserList == null || mChooserList.size() == 0)
	  {
		 mTvNoContent.setVisibility(View.VISIBLE);
		 mLvChooser.setVisibility(View.GONE);
	  }
	  else
	  {
		 mTvNoContent.setVisibility(View.GONE);
		 mLvChooser.setVisibility(View.VISIBLE);
	  }
	  AdpList_ShareChooser adapter = new AdpList_ShareChooser(this, mChooserList);
	  mLvChooser.setAdapter(adapter);

	  mLvChooser.setOnItemClickListener(new OnItemClickListener()
	  {

		 @Override
		 public void onItemClick(AdapterView<?> parent, View v, int position, long id)
		 {
			HashMap<String, Object> chooserMap = mChooserList.get(position);
			mPassIntent.setClassName((String) chooserMap.get("AppPackageName"), (String) chooserMap.get("AppActivityName"));
			startActivity(mPassIntent);
			finish();
		 }
	  });
   }

   private boolean isActivityAccept(String actName)
   {
	  boolean result = true;
	  if (ACCEPT_LIST != null && ACCEPT_LIST.length > 0)
	  {
		 result = false;
		 for (String str : ACCEPT_LIST)
			if (str.equals(actName))
			{
			   result = true;
			   break;
			}
	  }
	  else if (IGNORE_LIST != null && IGNORE_LIST.length > 0)
	  {
		 result = true;
		 for (String str : IGNORE_LIST)
			if (str.equals(actName))
			   result = false;
	  }
	  return result;
   }

   public static Intent createChooser(Context context, Intent intent, String title)
   {
	  Intent newIntent = new Intent(context, UIShareChooser.class);
	  newIntent.putExtra(Intent.EXTRA_INTENT, intent);
	  if (title != null && !title.equals(""))
		 newIntent.putExtra(Intent.EXTRA_TITLE, title);
	  else
		 newIntent.putExtra("title", "好友分享");
	  return newIntent;
   }
}
